package HomeWork2CalculateFigureArea;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        printMenu();

        ArrayList<Figure> figures = new ArrayList<>();

        int answer = sc.nextInt();
        while (answer != 4) {
            switch (answer) {
                case 1:
                    Figure circular= new Circular();
                    add(circular,figures);
                    break;
                case 2:
                    Figure rectangle = new Rectangle();
                    add(rectangle,figures);
                    break;
                case 3:
                    calculateAll(figures);
                    break;
                default:
                    System.out.println("请重新选择");
                    break;
            }
            answer = sc.nextInt();
        }

    }
    public static void add(Figure figure , ArrayList<Figure> figures) {
        Scanner sc = new Scanner(System.in);

        if (figure instanceof Circular) {
            System.out.println("输入半径:");
            int r = sc.nextInt();
            if(r <= 0){
                System.out.println("添加失败，请重新选择图形并输入正确的半径或边长");
                return;
            }
            Circular circular = (Circular) figure;
            circular.setRadius(r);
            figures.add(circular);
            System.out.println("添加成功");
            printMenu();
        } else if (figure instanceof Rectangle) {
            System.out.println("输入长和宽:");
            int h = sc.nextInt();
            int w = sc.nextInt();
            if(h <= 0 || w <= 0){
                System.out.println("添加失败，请重新选择图形并输入正确的半径或边长");
                return;
            }
            Rectangle rectangle = (Rectangle) figure;
            rectangle.setHeight(h);
            rectangle.setWidth(w);
            figures.add(rectangle);
            System.out.println("添加成功");
            printMenu();
        }
    }
    public static void calculateAll(ArrayList<Figure> figures) {
        for (int i = 0; i < figures.size(); i++) {
            Figure figure = figures.get(i);
            if (figure instanceof Circular) {
                Circular circular = (Circular) figure;
                System.out.println("圆形面积: " + circular.getArea());
            }else if (figure instanceof Rectangle) {
                Rectangle rectangle = (Rectangle) figure;
                System.out.println("矩形面积: " + rectangle.getArea());
            }
        }
    }
    public static void printMenu() {
        System.out.println("1.添加圆形 2. 添加矩形  3. 计算所有面积 4. 退出");
    }
}
